<?php
abstract class NoBusinessBase_Controller_Oslc_ActionBase extends Zend_Controller_Action
{
   protected $_dom = null;
   protected $_nextAppendToElement = null;
   
   public function init()
   {
      $request    = $this->getRequest();
      $header     = $request->getHeader('Accept');
      switch (true)
      {
         // Depending on the value, set the correct format
         case (strstr($header, '+json')) :
            $request->setParam('format', 'json');
            break;
         case (strstr($header, '+xml')) :
         case (strstr($header, 'rdf+xml')) :
            $request->setParam('format', 'xml');
            $this->getResponse()->setheader('Content-Type', 'text/xml');
            Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
            Zend_Layout::getMvcInstance()->disableLayout();
            break;
         case (strstr($header, '+html')) :
            $request->setParam('format', 'html');
            break;
         default:
            // Default: return whatever is default, but only when the format is not set
            $format = $request->getParam('format');
            if (! isset ($format)) {
               /* Format is not found, so we use HTML. Used so we don't need to specify
                * a format when checking the REST server at the browser */
               $request->setParam('format', 'html');
            }
            break;
      }
      $this->createDOM();
   }

   protected function displayDom()
   {
      $request    = $this->getRequest();
      $output     = '';
      switch($request->getParam('format'))
      {
         case 'xml':
            $output = $this->_dom->saveXML();
            break;
      }
      $this->_response->setBody($output);
   }

   private function createDOM()
   {
      $this->_dom = new DOMDocument('1.0', 'utf-8');
      $this->_nextAppendToElement = $this->_dom;
   }
   
   protected function attrAppend(&$element, $type, $value = '')
   {
      if($value != '')
      {
         $element->setAttribute($type, $this->_helper->valueReplace()->replaceValue($value));
         return;
      }
      
      switch($type)
      {
         case 'rdf:about':       $element->setAttribute('rdf:about', 'http://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri()); break;
         case 'rdf:resource':    $element->setAttribute('rdf:resource', 'http://open-services.net/ns/cm#'); break;
         case 'xmlns:rdf':       $element->setAttribute('xmlns:rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); break;
         case 'xmlns:dc':        $element->setAttribute('xmlns:dc', "http://purl.org/dc/terms/"); break;
         case 'xmlns:dcterms':   $element->setAttribute('xmlns:dcterms', "http://purl.org/dc/terms/"); break;
         case 'xmlns:oslc':      $element->setAttribute('xmlns:oslc', "http://open-services.net/ns/core#"); break;
      }
   }
   
   public function createRdfElement()
   {
      $rdf = $this->_dom->createElement('rdf:RDF');
      $this->attrAppend($rdf, 'xmlns:oslc');
      $this->attrAppend($rdf, 'xmlns:dcterms');
      $this->attrAppend($rdf, 'xmlns:rdf');
      
      $this->_dom->appendChild($rdf);
      $this->_nextAppendToElement = $rdf;
      
      return $rdf;
   }
   
}

